import { object } from '@/lib/utils/validate';
export default {
    data() {
        return {
            pageDatas: [],
            list: [],
            showDialog: false,
            type: '',
            row: {},
            rows: [],
            formData: {
                value: "",
            },
            searchUrl: "",
            deleteUrl: "",
        }
    },
    created() {
        try {
            const datas = this.$root._route.meta.datas;
            // const apiModule = this.$root._route.meta.apiModule;
            this.pageDatas = datas;
            // this.searchUrl = `${this.apiModule || apiModule}_list`;
            // this.deleteUrl = `${this.apiModule || apiModule}_delete`;
        } catch (e) {
            this.openMessage(JSON.stringify(e), 'warning');
        }
        this.search();
    },
    methods: {
        handleAdd(e) {
            this.type = '新增';
            if (e && object(e)) this.row = e;
            this.showDialog = true;
        },
        handleEdit(e) {
            this.type = '修改';
            this.row = e;
            this.showDialog = true;
        },
        handleSuccess(e) {
            this.search();
            this.showDialog = false;
        },
        search() {
            this.row = {};
            if(!this.searchUrl)return;
            try {
                this.$api[this.searchUrl](this.formData).then((data) => {
                    if (object(data)) {
                        this.list = data.data;
                    } else {
                        this.list = data;
                    }
                    if (this.addName) {
                        this.addName(this.list);
                    }
                });
            } catch (e) {
                //TODO handle the exception
                this.openMessage(`未定义接口 ${this.searchUrl}`, 'warning');
            }
        },
        handleDelete(e) {
            this.row = e;
            this.openConfirm('是否确认删除?', () => {
                this.$api[this.deleteUrl]({ id: this.row.id }).then(() => {
                    this.search();
                });
            })
        },
    }
}
